﻿Imports System.Data

Public Class FrmSectorListado

    Private Sub FrmSectorListado_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dgSectores.AutoGenerateColumns = False
        CargarSector()
        llenararea()
    End Sub

    Dim classController As New Class_RC_Controller.rc_Mantenimiento()

    Private m_frmSector As FrmSector
    Public Property frmSector() As FrmSector
        Get
            Return m_frmSector
        End Get
        Set(ByVal value As FrmSector)
            m_frmSector = value
        End Set
    End Property

    Private Sub CargarSector()
        Dim CodArea As Integer
        If cbxArea.Checked Then
            CodArea = cmbarea.SelectedValue
        End If
        Dim dt As New DataTable
        dt = classController.get_sector_All(oiml.dbConn, 0, txtDescripcion.Text, CodArea)
        dgSectores.DataSource = dt
    End Sub
    Private Sub llenararea()
        Dim dsArea As New DataTable
        dsArea = classController.get_Area_All(oiml.dbConn, 0, "")
        If Not dsArea Is Nothing Then
            If dsArea.Rows.Count > 0 Then
                cmbarea.DataSource = dsArea
                cmbarea.DisplayMember = "descArea"
                cmbarea.ValueMember = "codArea"
            End If
        End If
    End Sub
    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        Me.Close()

    End Sub

    Private Sub btnEditar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEditar.Click
        Try
            If dgSectores.SelectedRows.Count > 0 Then
                Dim CodSector As New Integer
                CodSector = dgSectores.SelectedRows(0).Cells(0).Value

                If frmSector Is Nothing Then
                    frmSector = New FrmSector
                    frmSector.codSector = CodSector
                    'If frmServicios.ShowDialog() = Windows.Forms.DialogResult.OK Then
                    frmSector.ShowDialog()
                    CargarSector()
                    frmSector.Dispose()
                    frmSector = Nothing
                    'End If
                Else
                    frmSector.codSector = CodSector
                    frmSector.ShowDialog()
                End If
            End If
        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub txtBuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBuscar.Click
        CargarSector()
    End Sub

    Private Sub txtDescripcion_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtDescripcion.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarSector()
        End If
    End Sub

    Private Sub dgSector_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgSectores.DoubleClick
        btnEditar_Click(sender, e)
    End Sub

    Private Sub dgSector_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dgSectores.KeyDown
        If e.KeyCode = Keys.Enter Then
            btnEditar_Click(sender, e)
        End If

    End Sub

    Private Sub btnNuevo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNuevo.Click
        '    If frmciudad Is Nothing Then
        frmSector = New FrmSector()
        frmSector.ShowDialog()
        CargarSector()
    End Sub
End Class